package com.turbo.cloud.core.annotations.dict.handler;


import com.turbo.cloud.core.annotations.dict.bean.BeanDescription;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 拆包工具类
 * @author zhangluning
 */
public class DefaultTranslateTypeHandler implements TranslateUnpackingHandler {

    /**
     * @param collection 列表
     * @param clazz 类
     * */
    @SuppressWarnings("unchecked")
    @Override
    public List<BeanDescription> unpackingCollection(Collection collection, Class<?> clazz) {
        return (List<BeanDescription>) collection.stream()
                .map(obj -> new BeanDescription(obj, clazz))
                .collect(Collectors.toList());
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<BeanDescription> unpackingMap(Map map, Class<?> clazz) {
        return (List<BeanDescription>) map.values().stream()
                .map(obj -> new BeanDescription(obj, clazz))
                .collect(Collectors.toList());
    }

    @Override
    public List<BeanDescription> unpackingArray(Object[] array, Class<?> clazz) {
        return Arrays.stream(array)
                .map(obj -> new BeanDescription(obj, clazz))
                .collect(Collectors.toList());
    }

    @Override
    public List<BeanDescription> unpackingOther(Object object, Class<?> clazz) {
        return Collections.emptyList();
    }

    @Override
    public int unpackingType(Object parsingObj) {
        if (parsingObj instanceof Collection) {
            return 1;
        }else if (parsingObj instanceof Map){
            return 2;
        }else if (parsingObj instanceof Object[]){
            return 3;
        }
        return 0;
    }
}
